// pages/card/card.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    id: '',
    data: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.data.id = options.id;
  },

  onShow: function () {

    const db = wx.cloud.database()
    let that = this;
    db.collection('card').where({
      _id: this.data.id // 填入当前用户 openid
    }).get({
      success: function (res) {

        if (res.data.length > 0) {
          that.setData({

            items: res.data[0],
            item: res.data
          })
        }
      }
    })
  },

  onEditor: function (e) {

    wx.navigateTo({
      url: '../editor/editor?id=' + e.currentTarget.dataset.id,
      success: function (res) {

      },
      fail: function (res) {

      },
      complete: function (res) { },
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 复制功能
  onCopy: function (e) {

    let cardInfo = this.data.items;
    wx.setClipboardData({
      data: cardInfo.name + '\n' + cardInfo.phone + '\n' + cardInfo.number,

      success: result => {

      },
      fail: () => { },
      complete: () => { }
    });
  },

  //删除卡片内容
  onDeleteTap: function (e) {
    wx.showModal({
      title: "提示",
      content: "确定要删除此卡片吗？",
      showCancel: true,
      confirmText: "删除",
      confirmColor: "#3CC51F",
      success: result => {

        if (result.confirm) {
          //把图片存到users集合表
          const db = wx.cloud.database();
          db.collection("card")
            .doc(this.data.id)
            .remove()
            .then(res => {
              wx.showToast({
                title: "卡片删除成功",
                icon: "none"
              });

              setTimeout(() => {
                wx.navigateBack({
                  delta: 1 // 回退前 delta(默认为1) 页面
                });
              }, 700);
            })
            .catch(error => { });
        }
      },
      fail: () => { },
      complete: () => { }
    });
  }


})